package capr.control;

import com.jme.input.InputHandler;
import com.jme.input.KeyBindingManager;
import com.jme.input.KeyInput;
import com.jme.input.action.KeyNodeBackwardAction;
import com.jme.input.action.KeyNodeForwardAction;
import com.jme.input.action.KeyNodeRotateLeftAction;
import com.jme.input.action.KeyNodeRotateRightAction;
import com.jme.scene.Spatial;

/**
 * @author Mark Powell
 * @author Gina Wagner
 * @author Tobias Bindler
 * 
 * ModelControl-Class based on the InputHandler by Mark Powell. Lets the User control the
 * player by hitting keys on the keyboard.
 *
 */
public class ModelControl extends InputHandler {

    /**
     * Mark Powell, Gina Wagner, Tobias Bindler <br>
     * 
     * Constructor creates a ModelControl-Object that is bound to a node and has a speed value for running forwards/backwards
     * and turning left/ right
     * 
     * @param node: the node we wish to move
     * @param forwardBackwardSpeed
     * @param turnRightLeftSpeed
     * 
     */
    public ModelControl(Spatial node, float forwardBackwardSpeed, float turnRightLeftSpeed) {
    	
        setKeyBindings();
        setActions(node, forwardBackwardSpeed, turnRightLeftSpeed);
    }

    /**
     * Gina Wagner, Tobias Bindler <br>
     *
     * creates the keyboard object, allowing us to obtain the values of a keyboard as keys are
     * pressed. It then sets the actions to be triggered based on if certain keys are pressed (UP, DOWN, RIGHT, LEFT).
     * 
     * @param api
     */
    private void setKeyBindings() {
    	
        KeyBindingManager keyboard = KeyBindingManager.getKeyBindingManager();

        keyboard.set("forward", KeyInput.KEY_UP);
        keyboard.set("backward", KeyInput.KEY_DOWN);
        keyboard.set("turnRight", KeyInput.KEY_RIGHT);
        keyboard.set("turnLeft", KeyInput.KEY_LEFT);
    }

    /**
     * Mark Powell, Gina Wagner, Tobias Bindler <br>
     * 
     * assigns action classes to triggers. These actions handle moving the node forward, backward and
     * rotating it.
     * 
     * @param node: the node to control.
     * @param forwardBackwardSpeed
     * @param turnRightLeftSpeed
     * 
     */
    private void setActions(Spatial node, float  forwardBackwardSpeed, float turnRightLeftSpeed) {
    	
    	KeyNodeForwardAction forward = new KeyNodeForwardAction(node, forwardBackwardSpeed);
        addAction(forward, "forward", true);
       
        KeyNodeBackwardAction backward = new KeyNodeBackwardAction(node, forwardBackwardSpeed);
        addAction(backward, "backward", true);
      
        KeyNodeRotateRightAction rotateRight = new KeyNodeRotateRightAction(node, turnRightLeftSpeed);
        rotateRight.setLockAxis(node.getLocalRotation().getRotationColumn(1));
        addAction(rotateRight, "turnRight", true);
       
        KeyNodeRotateLeftAction rotateLeft = new KeyNodeRotateLeftAction(node, turnRightLeftSpeed);
        rotateLeft.setLockAxis(node.getLocalRotation().getRotationColumn(1));
        addAction(rotateLeft, "turnLeft", true);
    }
}